package net.wy.commons.util;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-8-22
 * Time: 下午5:38
 * To change this template use File | Settings | File Templates.
 */
public class AssertExt extends Assert {
    public static void equals(String expected, String actual) {
        StringBuffer messageBuf = new StringBuffer();
        messageBuf.append("Expected:").append(expected).append("\r\n");
        messageBuf.append("Actual:").append(actual).append("\r\n");
        messageBuf.append("equals failure");
        equals(expected, actual, messageBuf.toString());
    }

    public static void equals(String expected, String actual, String message) {
        if (!org.apache.commons.lang.StringUtils.equalsIgnoreCase(expected, actual))
            throw new IllegalArgumentException(message);
    }

    public static void equals(String expected, String actual, SimpleException exception) throws SimpleException {
        if (!org.apache.commons.lang.StringUtils.equalsIgnoreCase(expected, actual))
            throw exception;
    }

    public static void equals(int expected, int actual, SimpleException exception) throws SimpleException {
        if (expected != actual)
            throw exception;
    }

    public static void isTrue(boolean expression, SimpleException exception) throws SimpleException {
        if (!expression)
            throw exception;
    }

    public static void isNull(Object object, SimpleException exception) throws SimpleException {
        if (object != null)
            throw exception;
    }

    public static void notNull(Object object, SimpleException exception) throws SimpleException {
        if (object == null)
            throw exception;
    }

    public static void hasLength(String text, SimpleException exception) throws SimpleException {
        if (!org.springframework.util.StringUtils.hasLength(text))
            throw exception;
    }

    public static void hasText(String text, SimpleException exception) throws SimpleException {
        if (!org.springframework.util.StringUtils.hasText(text))
            throw exception;
    }

    public static void doesNotContain(String textToSearch, String substring, SimpleException exception) throws SimpleException {
        if ((org.springframework.util.StringUtils.hasLength(textToSearch)) && (org.springframework.util.StringUtils.hasLength(substring)) && (textToSearch.indexOf(substring) != -1)) {
            throw exception;
        }
    }

    public static void notEmpty(Object[] array, SimpleException exception) throws SimpleException {
        if (ObjectUtils.isEmpty(array))
            throw exception;
    }

    public static void noNullElements(Object[] array, SimpleException exception) throws SimpleException {
        if (array != null)
            for (int i = 0; i < array.length; i++)
                if (array[i] == null)
                    throw exception;
    }

    public static void notEmpty(Collection collection, SimpleException exception)
            throws SimpleException {
        if (CollectionUtils.isEmpty(collection))
            throw exception;
    }

    public static void empty(Collection collection, SimpleException exception) throws SimpleException {
        if (!CollectionUtils.isEmpty(collection))
            throw exception;
    }

    public static void notEmpty(Map map, SimpleException exception) throws SimpleException {
        if (CollectionUtils.isEmpty(map))
            throw exception;
    }

    public static void isInstanceOf(Class type, Object obj, SimpleException exception) throws SimpleException {
        notNull(type, "Type to check against must not be null");
        if (!type.isInstance(obj))
            throw exception;
    }

    public static void isAssignable(Class superType, Class subType, SimpleException exception) throws SimpleException {
        notNull(superType, "Type to check against must not be null");
        if ((subType == null) || (!superType.isAssignableFrom(subType)))
            throw exception;
    }

    public static void state(boolean expression, SimpleException exception) throws SimpleException {
        if (!expression)
            throw exception;
    }
}
